package com.lzj.demo.leetcode;

import java.util.Scanner;

class Solution {
    public static void main(String[] args) {
         Scanner sc = new Scanner(System.in);
         System.out.println("输入一个罗马字符");
         String s = sc.nextLine();
        System.out.println("这个罗马数对应的整数是："+romaToInt(s));
     }
        public static int romaToInt(String s) {

            int sum = 0;
            int PreNum = getValue(s.charAt(0));
            for(int i = 1;i<s.length();i++){
                int num = getValue(s.charAt(i));
                if(PreNum<num){
                    sum-=PreNum;
                }else {
                    sum+=PreNum;
                }
                PreNum = num;
            }
                sum+=PreNum;
            return  sum;
        }
        public static int getValue(char ch){
            switch (ch){
                case 'I':
                    return 1;
                case 'V':
                    return 5;
                case 'X':
                    return 10;
                case 'L':
                    return 50;
                case 'C':
                    return 100;
                case 'D':
                    return 500;
                case 'M':
                    return 1000;
                default:
                    return 0;
            }

        }


 }
